var that;
class Tab {
    constructor(id) {
        //获取元素
        that = this;
        this.main = document.querySelector(id);
        this.add = this.main.querySelector('.tabadd');
        //li的父元素
        this.ul = this.main.querySelector('.fisrstnav ul:first-child');
        //section的父元素
        this.fsection = this.main.querySelector('.tabscon');
        this.init();
    }
    init() {
        // init 初始化
        this.updataNode();
        this.add.onclick = this.addTab;
        for (var i = 0; i < this.lis.length; i++) {
            this.lis[i].index = i;
            this.lis[i].onclick = this.toggleTab;
            this.remove[i].onclick = this.removeTab;
            this.spans[i].ondblclick = this.editTab;
            this.sections[i].ondblclick = this.editTab;
        }
    }
    //获取所有li和section  需要动态添加元素 所以要重新获取元素
    updataNode() {
        this.lis = this.main.querySelectorAll("li");
        this.sections = this.main.querySelectorAll("section");
        this.remove = this.main.querySelectorAll('.icon-guanbi');
        this.spans = this.main.querySelectorAll('.fisrstnav li span:first-child');
    }
    //1.切换功能
    toggleTab() {
        // console.log(this.index);
        that.clearClass();
        this.className = "liactive";
        that.sections[this.index].className = 'conactive';
    }
    //清除所有li和section类
    clearClass() {
        for (var i = 0; i < this.lis.length; i++) {
            this.lis[i].className = "";
            this.sections[i].className = "";
        }
    }
    //2.添加功能
    addTab() {
        that.clearClass();
        //创建li元素 和 section元素；
        var random = Math.random();
        var li = '<li class="liactive"><span>新选项卡</span><span class="iconfont icon-guanbi"></span></li>';
        var section = '<section class="conactive">测试' + random + '</section>';
        //追加到对应的父元素中
        that.ul.insertAdjacentHTML('beforeend', li);
        that.fsection.insertAdjacentHTML('beforeend', section);
        that.init();

    }
    //3.删除功能
    removeTab(e) {
        e.stopPropagation(); //阻止冒泡 
        var index = this.parentNode.index;
        // console.log(index);
        that.lis[index].remove();
        that.sections[index].remove();
        that.init();
        //当删除非选定状态的li时  原选定状态的li不变
        if (document.querySelector('.liactive')) return;
        //当我们删除了选定状态 li 的时候 让它前一个li处于选定状态
        index--;
        that.lis[index] && that.lis[index].click();
    }
    //4.修改功能
    editTab() {
        var str = this.innerHTML;
        //双击禁止选定状态
        window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
        this.innerHTML = '<input type = "text"/>'
        var input = this.children[0];
        input.value = str;
        input.select(); //让文本框的文字处于选定状态
        //当离开 文本框  把文本框的值给span
        input.onblur = function () {
            this.parentNode.innerHTML = this.value;
        };
        //按下回车把文本值给span
        input.onkeyup = function (e) {
            if (e.keyCode === 13) {
                this.blur();
            }
        }
    }
}
new Tab("#tab");